package noncomunicating.classes.SSI;

import java.util.ArrayList;

import util.Regex;

import noncomunicating.classes.Request;

public class SSIParser extends abstracts.Step{
	private ArrayList<String> SSITags = new ArrayList<String>();
	String fileString;

	public SSIParser(Request request) {
		super(request);
		fileString = new String(request.getFileContent());
		
		for( String cmt : Regex.allMatches("<\\!--.*-->", fileString)){
			parseComments(cmt);
		}
		
		updateRequestHTML();
		
	}
	
	private void parseComments(String cmt){
		if(cmt.contains("#include")){
			new Include(cmt, this);
			SSITags.add(cmt);
		}
		else if(cmt.contains("#echo")){
			new Echo(cmt, this);
			SSITags.add(cmt);
		}
	}
	
	private void updateRequestHTML(){
		request.setFileContent(fileString.getBytes());
	}
	
	//Output
	@Override
	public void printOutput(boolean verbose) {
		System.out.println(this.getOutput(verbose));
	}

	@Override
	public String getOutput(boolean verbose) {
		String retString = null;
		if(verbose){
			retString += (request.getName() + ": SSI Handled\n");
			retString += "Tags Handeld: " + this.SSITags.toString() + "\n";
		}
		else{
			retString = "SSI Handled.\n";
		}
		return retString;
	}

}
